<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="菜单名称">
              <a-input v-model="queryParam.name" placeholder="请输入"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button v-action:query type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button v-action:query style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button v-action:add type="primary" icon="plus" @click="$refs.modal.add()">新建</a-button>
    </div>

    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :indentSize="40"
    >
      <!--<template slot="sort" slot-scope="text, record">
        <editable-cell :text="text" @change="onCellChange($event, record.id, 'sort')"/>
      </template>-->

      <span slot="actions" slot-scope="text, record">
        <a-tag v-for="(action, index) in record.actions" :key="index">
          {{ action | actionFilter}}
        </a-tag>
      </span>

      <span slot="action" slot-scope="text, record">
        <template>
          <a v-action:update @click="$refs.modal.edit(record)">编辑</a>
          <a-divider type="vertical"/>
          <a v-action:delete @click="handleDel(record)">删除</a>
        </template>
      </span>
    </s-table>

    <Edit ref="modal" @ok="handleOk"></Edit>

  </a-card>
</template>

<script>
  import STable from '@/components/table/'
  import EditableCell from '@/components/table/EditableCell'
  import Edit from './Edit'
  import { selectPage, del, updateOther } from '@/api/system/menu'

  export default {
    components: {
      STable,
      EditableCell,
      Edit
    },
    //--############  data() ###############-->
    data() {
      return {
        description: '',
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '菜单名称',
            width: '15%',
            dataIndex: 'name',
            scopedSlots: { customRender: 'name' }
          },
          {
            title: '权限唯一键',
            width: '10%',
            dataIndex: 'keySign'
          },
          {
            title: '菜单图标',
            width: '10%',
            dataIndex: 'icon'
          },
          /*{
            title: 'component',
            width: '15%',
            dataIndex: 'component'
          },
          {
            title: '排序',
            width: '15%',
            dataIndex: 'sort',
            scopedSlots: { customRender: 'sort' }
          },*/
          {
            title: '可操作权限',
            width: '30%',
            dataIndex: 'actions',
            scopedSlots: { customRender: 'actions' }
          },
          {
            title: '操作',
            width: '15%',
            key: 'action',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return selectPage(Object.assign(parameter, this.queryParam))
            .then(res => {
              return res.result
            })
        },

        selectedRowKeys: [],
        selectedRows: []
      }
    },
    //--############  methods() ###############-->
    methods: {
      handleDel(row) {
        const _this = this
        this.$confirm({
          title: '警告',
          content: `真的要删除 ${row.name} 吗?`,
          okText: '删除',
          okType: 'danger',
          cancelText: '取消',
          onOk() {
            // 在这里调用删除接口
            del(row.id).then(res => {
              _this.$message.info('删除成功')
              _this.$refs.table.refresh()
            })
          },
          onCancel() {
            console.log('Cancel')
          }
        })
      },
      handleOk() {
        // 新增/修改 成功时，重载列表
        this.$refs.table.refresh(true)
      },

      onSelectChange(selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      toggleAdvanced() {
        this.advanced = !this.advanced
      },

      onCellChange (value, id, dataIndex) {
        const _this = this
        updateOther({id: id, name: dataIndex, value: value})
          .then(res => {
            _this.$message.info(res.message)
            _this.$refs.table.refresh()
          })
      },
    },
    //--############  methods() ###############-->
    //--############  filters() ###############-->
    filters: {
      actionFilter(state) {
        const statusMap = {
          'add': '新增',
          'query': '查询',
          'update': '修改',
          'delete': '删除',
          'import': '导入',
          'export': '导出',
          'other': '特殊',
        }
        return statusMap[state]
      }
    },
    //--############  filters() ###############-->
  }
</script>

<style>
  .editable-cell {
    position: relative;
  }

  .editable-cell-input-wrapper,
  .editable-cell-text-wrapper {
    padding-right: 24px;
  }

  .editable-cell-text-wrapper {
    padding: 5px 24px 5px 5px;
  }

  .editable-cell-icon,
  .editable-cell-icon-check {
    position: absolute;
    right: 0;
    width: 20px;
    cursor: pointer;
  }

  .editable-cell-icon {
    line-height: 18px;

  }

  .editable-cell-icon-check {
    line-height: 28px;
  }

  .editable-cell:hover .editable-cell-icon {
    display: inline-block;
  }

  .editable-cell-icon:hover,
  .editable-cell-icon-check:hover {
    color: #108ee9;
  }

  .editable-add-btn {
    margin-bottom: 8px;
  }
</style>
